package volkov.ja.applogic2;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

import volkov.ja.applogic.ApplicationManager;
import volkov.ja.applogic.MovieHelper;
import volkov.ja.applogic.NavigationHelper;
import volkov.ja.applogic.UserHelper;
import volkov.ja.util.Browser;
import volkov.ja.util.DbUtilities;
import volkov.ja.util.PropertyLoader;
import volkov.ja.util.TestLogger;
import volkov.ja.util.TracingWebDriver;
import volkov.ja.webdriver.WebDriverFactory;

public class ApplicationManager2 implements ApplicationManager {

	// private static final String SCREENSHOT_FOLDER = "target/screenshots/";
	// private static final String SCREENSHOT_FORMAT = ".png";
	// properties map to be filled either with Maven generated values of with
	// defaults
	private static Map<String, String> propMap = new HashMap<String, String>();
	static {
		propMap.put("site.url", "");
		propMap.put("grid2.hub", "");
		propMap.put("browser.name", "");
		propMap.put("browser.version", "");
		propMap.put("browser.platform", "");
		propMap.put("user.username", "");
		propMap.put("user.password", "");
		propMap.put("database.host", "");
		propMap.put("database.port", "");
		propMap.put("database.dbname", "");
		propMap.put("database.user", "");
		propMap.put("database.password", "");
	}

	private static TestLogger LOG = new TestLogger(ApplicationManager.class, null);

	private String gridHubUrl;

	private Browser browser;
	private WebDriver driver;
	private String baseUrl;

	private DbUtilities db;

	private static String session;

	private UserHelper userHelper;
	private MovieHelper movieHelper;
	private NavigationHelper navHelper;


	public ApplicationManager2() {
		LOG.debug("Reading settings");
		// check if the .properties are set by maven. If not, load defaults
		Matcher m = Pattern.compile("^\\$\\{").matcher("");
		for (Entry<String, String> me : propMap.entrySet()) {
			String value = PropertyLoader.loadProperty(me.getKey());
			if (m.reset(value).find())
				value = PropertyLoader.loadProperty("default." + me.getKey());
			if (value == null)
				value = "";
			me.setValue(value);
		}

		baseUrl = propMap.get("site.url");
		gridHubUrl = propMap.get("grid2.hub");

		browser = new Browser();
		browser.setName(propMap.get("browser.name"));
		browser.setVersion(propMap.get("browser.version"));
		browser.setPlatform(propMap.get("browser.platform"));

		String username = propMap.get("user.username");
		String password = propMap.get("user.password");

		LOG.debug("Starting browser");
		driver = WebDriverFactory.getInstance(gridHubUrl, browser, username,
				password);

		session = ((RemoteWebDriver) driver).getSessionId().toString();
		LOG.setSessionId(getSessionId());

		driver = new TracingWebDriver(driver, getSessionId());
		LOG.debug("Browser started");
		// create db connection
		db = new DbUtilities();

		userHelper = new UserHelper2(this);
		movieHelper = new MovieHelper2(this);
		navHelper = new NavigationHelper2(this);

//		getNavigationHelper().openMainPage();
	}

	@Override
	public UserHelper getUserHelper() {
		return userHelper;
	}

	@Override
	public MovieHelper getMovieHelper() {
		return movieHelper;
	}

	@Override
	public NavigationHelper getNavigationHelper() {
		return navHelper;
	}

	protected WebDriver getWebDriver() {
		return driver;
	}

	protected String getBaseUrl() {
		return baseUrl;
	}

	public static String getSessionId() {
		return session;
	}
	
	public static TestLogger getLog() {
		return LOG;
	}

	@Override
	public void stop() {
		// quit browser
		if (driver != null) {
			LOG.debug("Quitting browser");
			driver.quit();
			LOG.debug("Browser quitted");
		}
		// close db connection
		LOG.debug("Closing database connection");
		db.close();
	}
	
	@Override
	public DbUtilities getDbUtilities() {
		return db;
	}
	
}
